package com.sifang.bussiness.manager.contorller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sifang.base.util.FileUtil;
import com.sifang.base.util.IPUtils;
import com.sifang.bussiness.base.BaseController;
import com.sifang.bussiness.dao.table.finance.FinancialSetting;
import com.sifang.bussiness.manager.service.ManagerNotifyService;
import com.sifang.bussiness.merchant.service.WhiteListIpService;

@RestController
public class ManagerNotifyController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(ManagerNotifyController.class);
	
	@Autowired
	ManagerNotifyService managerNotifyService;
	
	@Autowired
	WhiteListIpService whiteListIpService;
	
	@RequestMapping(value = "/notify/{type}/{className}")
	public void notifyTrade(HttpServletRequest req,HttpServletResponse rsp,@PathVariable("type") String type,@PathVariable("className") String className) {
		
		
		//判断是否开启IP拦截
		FinancialSetting financialSetting = whiteListIpService.getFinancialSetting();
		if(financialSetting.getChannel_white() == FinancialSetting.STATUS_OPEN) {
			//IP拦截
			String reqIp = IPUtils.getIpAddr(req);
			boolean ipResult = whiteListIpService.getChannelWhiteListCountByIp(reqIp);
			if(!ipResult) {
				write(rsp, "Illegal ip");
				return;
			}
		}
		
		String reqUrlName = req.getServletPath().toString();
		String msg = this.readReqStr(req);
		log.info("请求地址:{}",reqUrlName);
		String rspMsg = "";
		//先判断类名是否存在
		boolean existClassName = managerNotifyService.existClassName(className);
		if(!existClassName) {
			write(rsp, "Illegal Request");
			return;
		}
		if(type.equals("pay")) {
			try {
				rspMsg = managerNotifyService.parsePayNotify(msg, className);
			} catch (Exception e) {
				log.error(e.getMessage());
				rspMsg = "system error";
			}
			write(rsp, rspMsg);
		}else if(type.equals("withdrawal")) {
			try {
				rspMsg = managerNotifyService.parseWithDrawalNotify(msg, className);
			} catch (Exception e) {
				log.error(e.getMessage());
				rspMsg = "system error";
			}
			write(rsp, rspMsg);
		}else {
			write(rsp, "Illegal Request");
			return;
		}
		
		
	}

	public String readReqStr(HttpServletRequest request) {
		String reqJson = null;
		try {
			//log.info("请求类型：{}",request.getMethod());
			//log.info("尝试拿参数status：{}",request.getParameter("request"));
			request.setCharacterEncoding("UTF-8");
			//取消json报文字节流加密
			byte[] reqByte = FileUtil.streamToBytes(request.getInputStream());
			log.info("请求的报文的大小-->{}",reqByte.length);
			reqJson = new String(reqByte, "UTF-8");
			log.info("请求的报文：{}",reqJson);
		} catch (Exception e) {
			reqJson = null;
			log.error(e.getMessage());
		}
		return reqJson;
	}
}
